package com.meet.controller;

/**
 * @author RicardoZeng
 * @date 2022/8/11 22:25
 */

import com.meet.dao.DepartmentDao;
import com.meet.dao.DepartmentDaoImpl;
import com.meet.entity.BoardRoom;
import com.meet.entity.Department;
import com.meet.entity.PageBean;
import com.meet.service.DepartmentService;
import com.meet.service.DepartmentServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/servlet/DepartmentServlet")
public class DepartmentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Department department = new Department();
        DepartmentDao departmentDao = new DepartmentDaoImpl();
        DepartmentService departmentService = new DepartmentServiceImpl(departmentDao);

        String flag = request.getParameter("flag");


        if ("1".equals(flag)) {
            //1.获取参数
            String currentPage = request.getParameter("page");//当前页码
            String rows = request.getParameter("rows");//每页显示条数

            if (currentPage == null || "".equals(currentPage)) {

                currentPage = "1";
            }

            if (rows == null || "".equals(rows)) {
                rows = "6";
            }
            PageBean<Department> pb = null;

            try {
                pb = departmentService.getAllDptByPage(currentPage, rows);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

            //3.将PageBean存入request
            request.setAttribute("pb", pb);

            //4.转发到main.jsp
            request.getRequestDispatcher("departmentShow.jsp").forward(request, response);

        } else if ("2".equals(flag)) {

            String departmentName = request.getParameter("dName");

            Department departments = new Department(null, departmentName);
            try {
                int count = departmentService.addDepartment(departments);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

            request.getRequestDispatcher("DepartmentServlet?flag=1").forward(request, response);

        } else if ("3".equals(flag)) {

            Integer id = Integer.parseInt(request.getParameter("id"));

            try {
                department = departmentService.getDptId(id);
                request.setAttribute("department", department);

            } catch (SQLException e) {
                e.printStackTrace();
            }
            request.getRequestDispatcher("departmentEdit.jsp").forward(request, response);

        } else if ("4".equals(flag)) {

            Integer id = Integer.parseInt(request.getParameter("id"));
            String departmentName = request.getParameter("dName");

            Department departments = new Department(id, departmentName);

            try {
                departmentService.updateDepartment(departments);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

            request.getRequestDispatcher("DepartmentServlet?flag=1").forward(request, response);

        } else if ("5".equals(flag)) {

            Integer id = Integer.parseInt(request.getParameter("id"));

            try {
                departmentService.delDepartment(id);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

            request.getRequestDispatcher("DepartmentServlet?flag=1").forward(request, response);
        }

    }
}
